package org.ft.fileConvert.service.impl;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.net.NetUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.http.HttpUtil;
import lombok.extern.slf4j.Slf4j;
import org.ft.fileConvert.config.FilePathConfig;
import org.ft.fileConvert.service.AttachService;
import org.ft.fileConvert.utils.OfdAttachmentUtil;
import org.ft.fileConvert.vo.ResultVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by Chen Tianzhong in 2022/8/11
 */
@Service
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class AttachServiceImpl implements AttachService {

    @Resource
    private FilePathConfig filePathConfig;
    @Resource
    private OfdAttachmentUtil ofdAttachmentUtil;

    @Override
    public ResultVo addOfdAttachment(String ofdUrl, String attachmentFileUrl, String attachmentName, String ext) {
        String resultPath = "";

        if (StringUtils.hasText(ofdUrl) ){
            return ResultVo.fail("ofd文件路径为空");
        }
        if (!ofdUrl.toLowerCase().startsWith("http://") && ofdUrl.toLowerCase().startsWith("https://")) {
            return ResultVo.fail("ofd文件URL格式错误");
        }
        if (StringUtils.hasText(attachmentFileUrl))
            return ResultVo.fail("附件文件URL地址为空");
        if (!attachmentFileUrl.toLowerCase().startsWith("http://") && !attachmentFileUrl.toLowerCase().startsWith("https://")) {
            return ResultVo.fail("附件文件URL格式错误");
        }
        try {
            File tempPath = new File(filePathConfig.getLocalPath()+new SimpleDateFormat("yyyy/MM/dd/").format(new Date()));
            if (!tempPath.exists()){
                tempPath.mkdirs();
            }
            File filePath = new File(tempPath.getAbsoluteFile()+File.separator+ IdUtil.fastSimpleUUID()+".ofd");
            if (!filePath.exists()){
                FileUtil.touch(filePath);
            }
            long l = HttpUtil.downloadFile(ofdUrl, filePath);
            log.info("下载ofd文件用时{}毫秒", l);

            File attachPath = new File(filePathConfig.getTempPath()+new SimpleDateFormat("yyyy/MM/dd/").format(new Date()));
            if (!attachPath.exists()){
                attachPath.mkdirs();
            }
            File attachFile = new File(attachPath.getAbsoluteFile()+File.separator+ IdUtil.fastSimpleUUID()+"."+ext);
            if (!attachFile.exists()){
                FileUtil.touch(attachFile);
            }
            long l1 = HttpUtil.downloadFile(ofdUrl, filePath);
            log.info("下载附件对象文件用时{}毫秒", l);

            String path  = ofdAttachmentUtil.attachFileIntoOFD(filePath.getAbsolutePath(), attachFile.getAbsolutePath(), attachmentName);
            Assert.hasText(path, "替换ofd附件失败, 返回值为空");
            resultPath = path.replaceAll(filePathConfig.getPath(), filePathConfig.getUrlBase());
        }catch (Exception e){
            log.error("替换ofd附件失败");
            log.error(e.getLocalizedMessage(), e);
            return ResultVo.fail("替换ofd附件失败");
        }
        return ResultVo.success(resultPath);
    }
}
